# Replication code for Claudia J. Kim and Taylor C. Boas, "Activist Disconnect: Social Movements, Public Opinion, and U.S. Military Bases in East Asia," Armed Forces and Society.

# Analysis conducted in R 3.6.0 on MacOS 10.13.6

# NOTE: This file reads, cleans, and combines the raw survey data.

# Set working directory as appropriate
# setwd('~/Dropbox/us_bases_experiment/replication/')

# Clean desktop and load raw data files
rm(list=ls(all=T))

load('japan_raw.RData')
load('korea_raw.RData')

# Eliminate respondents who did not make it through the last demographic question in the survey. 
japan<-japan[japan$Q35!='',]
korea<-korea[korea$Q35!='',]

# Country and Region
japan$Country<-'Japan'
korea$Country<-'Korea'
japan$Q5<-ifelse(japan$Q5==14,'Kanagawa','Okinawa')
korea$Q5<-ifelse(korea$Q5==21, 'Daegu','Gyeonggi')

# Experimental question response
japan$Q30<-apply(japan[,grep('Q30',names(japan))],1,function(x) sum(as.numeric(x),na.rm=T))
korea$Q30<-apply(korea[,grep('Q30',names(korea))],1,function(x) sum(as.numeric(x),na.rm=T))

# Treatment condition indicator
japan$Q30_cond<-1*(japan$Q30a!='')+2*(japan$Q30b!='')+3*(japan$Q30c_o!=''|japan$Q30c_k!='')+4*(japan$Q30d_o!=''|japan$Q30d_k!='')+5*(japan$Q30e!='')+6*(japan$Q30f!='')+7*(japan$Q30g!='')+8*(japan$Q30h_o!=''|japan$Q30h_k!='')
korea$Q30_cond<-1*(korea$Q30a!='')+2*(korea$Q30b!='')+3*(korea$Q30c_d!=''|korea$Q30c_g!='')+4*(korea$Q30d_d!=''|korea$Q30d_g!='')+5*(korea$Q30e!='')+6*(korea$Q30_f!='')+7*(korea$Q30g!='')+8*(korea$Q30h_d!=''|korea$Q30h_g!='')

# Eliminating those from Korea who, due to error, never got a treatment.
korea<-korea[korea$Q30_cond>0,]

# Eliminate unneeded treatment columns and combine
korea$Q30a<-korea$Q30b<-korea$Q30c_d<-korea$Q30c_g<-korea$Q30d_d<-korea$Q30d_g<-korea$Q30e<-korea$Q30_f<-korea$Q30g<-korea$Q30h_d<-korea$Q30h_g<-NULL
japan$Q30a<-japan$Q30b<-japan$Q30c_o<-japan$Q30c_k<-japan$Q30d_o<-japan$Q30d_k<-japan$Q30e<-japan$Q30f<-japan$Q30g<-japan$Q30h_o<-japan$Q30h_k<-NULL

basedata<-rbind(korea,japan)

# Start and end date/time
basedata$StartDate<-as.POSIXct(basedata$StartDate,tz='America/New_York')
basedata$EndDate<-as.POSIXct(basedata$EndDate,tz='America/New_York')

# Clean other variables
names(basedata)[which(names(basedata)=='Duration..in.seconds.')]<-'Duration'
names(basedata)[grep('B1',names(basedata))]<-c('Browser_type','Browser_version','OS','Screen_res')
basedata$Q1<-as.numeric(basedata$Q1)
basedata$Q2<-as.numeric(basedata$Q2)
basedata$Q3<-as.numeric(basedata$Q3)
basedata$Q4<-as.numeric(basedata$Q4)+16
basedata$Q6<-as.numeric(basedata$Q6)
basedata$Q7<-as.numeric(basedata$Q7)
basedata$Q8<-as.numeric(basedata$Q8)
basedata$Q9<-as.numeric(basedata$Q9)
basedata$Q10<-as.numeric(basedata$Q10)
basedata$Q11<-as.numeric(basedata$Q11)

# Party voted for: Korean parties will be numbers 1-10, Japanese parties 11-18, Other party 19, Did not vote 20. Dealing with screwy numbers in original Korea data file.
basedata$Q12[basedata$Country=='Japan']<-as.numeric(basedata$Q12[basedata$Country=='Japan'])+10
basedata$Q12[basedata$Country=='Korea'& basedata$Q12 %in% 7:10]<-as.numeric(basedata$Q12[basedata$Country=='Korea'& basedata$Q12 %in% 7:10])-6
basedata$Q12[basedata$Country=='Korea'& basedata$Q12 %in% 15:17]<-as.numeric(basedata$Q12[basedata$Country=='Korea'& basedata$Q12 %in% 15:17])-7
basedata$Q12[basedata$Country=='Korea'& basedata$Q12==13]<-5
basedata$Q12[basedata$Country=='Korea'& basedata$Q12==20]<-6
basedata$Q12[basedata$Country=='Korea'& basedata$Q12==14]<-7
basedata$Q12<-as.numeric(basedata$Q12)
basedata$Q12[basedata$Country=='Korea'& basedata$Q12 %in% 11:12]<-as.numeric(basedata$Q12[basedata$Country=='Korea'& basedata$Q12 %in% 11:12])+8

basedata$Q13<-as.numeric(basedata$Q13)

# Ideological placement of organizations: renumbering Korean organizations Q14-Q17 (numbers unused because of eliminating placement of party questions)
basedata$Q14<-as.numeric(basedata$Q18)
basedata$Q14[basedata$Country=='Japan']<-NA
basedata$Q15<-as.numeric(basedata$Q19)
basedata$Q15[basedata$Country=='Japan']<-NA
basedata$Q16<-as.numeric(basedata$Q20)
basedata$Q16[basedata$Country=='Japan']<-NA
basedata$Q17<-as.numeric(basedata$Q21)
basedata$Q17[basedata$Country=='Japan']<-NA

basedata$Q18<-as.numeric(basedata$Q18)
basedata$Q18[basedata$Country=='Korea']<-NA
basedata$Q19<-as.numeric(basedata$Q19)
basedata$Q19[basedata$Country=='Korea']<-NA
basedata$Q20<-as.numeric(basedata$Q20)
basedata$Q20[basedata$Country=='Korea']<-NA
basedata$Q21<-as.numeric(basedata$Q21)
basedata$Q21[basedata$Country=='Korea']<-NA

basedata$Q22<-as.numeric(basedata$Q22)
basedata$Q23<-as.numeric(basedata$Q23)

basedata$Q25<-as.numeric(basedata$Q25) - 14
basedata$Q25_firstclick<-as.numeric(basedata$T25_First.Click)
basedata$Q25_lastclick<-as.numeric(basedata$T25_Last.Click)
basedata$Q25_submit<-as.numeric(basedata$T25_Page.Submit)
basedata$Q25_clickcount<-as.numeric(basedata$T25_Click.Count)
basedata$T25_First.Click<-basedata$T25_Last.Click<-basedata$T25_Page.Submit<-basedata$T25_Click.Count<-NULL

basedata$Q28<-as.numeric(basedata$Q28)
basedata$Q29<-as.numeric(basedata$Q29)
basedata$Q31<-as.numeric(basedata$Q31) # Coding on this one got screwy
basedata$Q31[basedata$Q31==3]<-4
basedata$Q31[basedata$Q31==6]<-3
basedata$Q32<-as.numeric(basedata$Q32)
basedata$Q33<-as.numeric(basedata$Q33)
basedata$Q34<-as.numeric(basedata$Q34)
basedata$Q35<-as.numeric(basedata$Q35)

# The small number who quit on the question about entering the raffle will be treated as answering "no."
basedata$Q36[basedata$Q36=='']<-2
basedata$Q36<-as.numeric(basedata$Q36)

# Those who quit on the question about donating money will be treated as donating zero. Scale is 0-1, calculated separately for each country.
basedata$Q37[basedata$Q36==2]<-NA
basedata$Q37[basedata$Q37=='']<-0
basedata$Q37<-as.numeric(gsub(',','', basedata$Q37))
basedata$Q37[basedata$Country=='Korea']<-basedata$Q37[basedata$Country=='Korea']/50000
basedata$Q37[basedata$Country=='Japan']<-basedata$Q37[basedata$Country=='Japan']/5000

# Reorder columns and save
basedata<-basedata[,c('StartDate','EndDate','Duration','Browser_type','Browser_version','OS','Screen_res','Country',paste('Q',1:23,sep=''),'Q25','Q25_firstclick','Q25_lastclick','Q25_submit','Q25_clickcount', paste('Q',28:30,sep=''),'Q30_cond', paste('Q',31:37,sep=''))]

save(basedata,file='basedata_cleaned.RData')
